import java.util.ArrayList;

public class TextBoxArtist
{
	public int x;
	public int y;
	public int width = 150;
	public String text;
	public String emptyTextBox;
	public ScrollBar scrollBar;
	public boolean active;

	public ArrayList<ButtonArtist> suggestionButtonsToShow;


	public float buttonTimer;
	public int heightVal;
	public int suggestionHeight;
	
	ThreadExample p;



	////////////////////////////////////////////////////////////////////////////////////////////////////
	public TextBoxArtist(String emptyTextBox)
	{
		text = "";
		scrollBar = new ScrollBar();
		active=false;
		this.emptyTextBox = emptyTextBox;
		suggestionButtonsToShow = new ArrayList<ButtonArtist>();
		updateButtons();
		
		p = new ThreadExample();
	}

	public void draw()
	{
		// If not in graph mode don't show the textbox
		if(GLOBAL.analysisType != 2) return;

		GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
		GLOBAL.processing.noFill();
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(2);
		GLOBAL.processing.rect(x, y, width, 30);

		GLOBAL.processing.fill(GLOBAL.textColor);	
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);

		suggestionHeight = 130;

		if(active){
			if(buttonTimer==(float)0.0){
				scrollBar.val=0;
				buttonTimer = GLOBAL.timer;
				heightVal=0;
			}
			heightVal = (int)GLOBAL.processing.lerp(heightVal,suggestionHeight,(float) 0.3);
			drawSuggestions();
		}
		else{if(buttonTimer!=(float)0.0){heightVal=suggestionHeight;}
		heightVal = (int)GLOBAL.processing.lerp(heightVal,0,(float) 0.3);
		if(heightVal!=0)
			drawSuggestions();
		else 
			GLOBAL.processing.text(emptyTextBox, x + 5, y + 20);

		buttonTimer=(float)0.0;

		}
//		if(p.isAlive()){
//			GLOBAL.angleArtist = GLOBAL.loadingCircle(700,697,GLOBAL.angleArtist);
//		System.out.println("loading");
//		}
	}

	public void drawSuggestions(){
		GLOBAL.processing.text(text, x + 5, y + 20);
		GLOBAL.processing.fill(44,160);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.strokeWeight(1);
		GLOBAL.processing.rect(x, y+30, width, heightVal);


		scrollBar.width = 15;
		scrollBar.x =  x +width-15;
		scrollBar.y = y+30;
		scrollBar.height = heightVal-4;
		if(heightVal>scrollBar.y-y && suggestionButtonsToShow.size()>4){
			scrollBar.draw();  
			scrollBar.size = 1/(float)(suggestionButtonsToShow.size()-2);
		}
		showButtons();

	}


	public void keyTyped()
	{
		if(GLOBAL.processing.key==GLOBAL.processing.ENTER){

			if(suggestionButtonsToShow.size()>0){
				Artist a =suggestionButtonsToShow.get(0).artist;
				GLOBAL.artistAddRemove(a);
				this.active=false;
				text = new String();
			}
		}
		else if(GLOBAL.processing.key == 8)
		{
			if(text.length() > 0) text = text.substring(0, text.length() - 1);
		}
		else if(text.length()>16){
			text = text.substring(0, text.length() - 1);
			text += GLOBAL.processing.key;
		}
		else
		{
			text += GLOBAL.processing.key;
		}

		updateButtons();

	}


	public boolean mouseOver()
	{
		if(GLOBAL.processing.mouseX > x && GLOBAL.processing.mouseX < x + width && 
				GLOBAL.processing.mouseY > y && GLOBAL.processing.mouseY < y + 30) return true;
		return false;
	}

	public boolean mouseOverSuggestions(){

		if(GLOBAL.processing.mouseX > x && GLOBAL.processing.mouseX < x + width && 
				GLOBAL.processing.mouseY > y+30 && GLOBAL.processing.mouseY < y + 165)return true;
		return false;
	}

	//show  buttons in the suggestions
	private void showButtons() {

		float j = Main.map (scrollBar.val,0,1,0,suggestionButtonsToShow.size()-3);
		if(j<0)
			j=0;

		for(int i=(int)j;i<=(int)j+5 && i<suggestionButtonsToShow.size()&&i-(int)j<+heightVal/23-0.3;i++){

			ButtonArtist b = suggestionButtonsToShow.get(i);
			if(b.state){
				if(suggestionButtonsToShow.size()>i){
					Artist a =suggestionButtonsToShow.get(i).artist;
					GLOBAL.artistAddRemove(a);
					this.active=false;
					text = new String();
				}
				updateButtons();
				break;
			}else{
				b.y=y+30+(i-(int)j)*25;
				b.draw();
			}
		}		
	}


	public void updateButtons(){
		suggestionButtonsToShow = new ArrayList<ButtonArtist>();

		if(text.length()<=1)return;

		if(p.isAlive())p.stop();
	    p = new ThreadExample();
		p.start();

	}

	public void mousePressed() {
		float j = Main.map (scrollBar.val,0,1,0,suggestionButtonsToShow.size()-3);
		if(j<0)
			j=0;
		for(int i=(int)j;i<=(int)j+5 && i<suggestionButtonsToShow.size()&&i-(int)j<+heightVal/23-0.3;i++){
			ButtonArtist b = suggestionButtonsToShow.get(i);
			if(b.mouseOver())b.mouseClicked();
		}
	}

}